/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jard.webshop.jsfbeans;

import jard.webshop.management.OrderManagement;
import jard.webshop.nbp.Invoice;
import jard.webshop.nbp.InvoiceEntry;
import jard.webshop.nbp.Order;
import jard.webshop.util.FinancialException;
import jard.webshop.util.Utils;
import java.io.Serializable;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;

/**
 *
 * @author CJP
 */
@ManagedBean(name = "orderBackingBean")
@SessionScoped // Suboptimal, I know
public class OrderBackingBean implements Serializable {
    
    private OrderManagement om = OrderManagement.getInstance();
    private Order order = null;
    private Invoice invoice = null;
    private Float paymentAmount = 0f;
    public Boolean panelCollapsed = true;

    /** Creates a new instance of OrderBackingBean */
    public OrderBackingBean() {
        System.out.println("Constructor of orderBackingBean");
    }
    
    public String view(Order order) {
        System.out.println("asking to view order: " + order);
        this.order = order;
        this.invoice = order.getInvoice();
        panelCollapsed = true;
        return "vieworder";
    }
    
    public Long getTrackingNumber() {
        return order.getTrackingNumber();
    }
    
    public Order getOrder() {
        return order;
    }

    public void setOrder(Order order) {
        this.order = order;
    }
    
    public List<InvoiceEntry> getEntries() {
        return order.getInvoice().getEntries();
    }
    
    public Float getTotalAmount() {
        return invoice.getTotalAmount();
    }
    
    public Float getPaidAmount() {
        return invoice.getPaidAmount();
    }
    
    public Float getOutstanding() {
        return invoice.getTotalAmount()-invoice.getPaidAmount();
    }
    
    public void doPayment() {
        try {
            om.registerPayment(invoice, paymentAmount);
        } catch (FinancialException ex) {
            System.err.println("Financial Exception when trying to make payment: " + ex);
            Utils.growl("Failed",ex.getMessage());
        }
    }

    public Float getPaymentAmount() {
        return paymentAmount;
    }

    public void setPaymentAmount(Float paymentAmount) {
        this.paymentAmount = paymentAmount;
    }
    
    public Boolean isPaid() {
        return invoice.isPaid();
    }
    
}
